package com.njegovanobradovic.icet.pages;

import java.util.List;

import org.apache.tapestry5.annotations.Component;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Log;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.corelib.components.Form;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.njegovanobradovic.icet.facade.Controller;
import com.njegovanobradovic.icet.metamodel.Chat;
import com.njegovanobradovic.icet.metamodel.User;
import com.njegovanobradovic.icet.metamodel.beans.ChatBean;

public class Details {
	@Persist
	private User user;
	@Inject
	private Controller controller;


	@Component
	private Form deleteForm;
	
	@InjectPage
	private Chating chating;

	public void setUser(User user) {
		this.user = user;
	}

	public User getUser() {
		return user;
	}

	public Object onActivate(){
		List<Chat> list = controller.getChatsOfUser(user);
		for (Chat chat : list) {
			System.out.println(chat.getRoom().getName());
		}
			return controller.isLoggedIn() ? null : Signin.class;
	}
	
	@Log
	public Object onSubmitFromDeleteForm() {
		controller.deleteUser(controller.getLoggedUser().getUsername(),
				controller.getLoggedUser().getPassword());
		controller.userLogout();
		return Index.class;
	}

	@Log
	public boolean getIsTheOne() {
		if (controller.getLoggedUser().equals(user)) {
			return true;
		}
		return false;
	}
	
	@OnEvent(component = "chatLink")
	Object onShowChat(){
		Chat chat = new ChatBean();
		controller.createChat(chat, controller.getLoggedUser(), user);
		controller.addWelcomeMessageToChat("Welcome to ICET! You send message to "+user.getName()+". Wait for him to answer!", chat);
		chating.setChat(chat);
		return chating;
	}
}
